package com.wallpaper.weatherwallpaper;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class UserImageAdapter extends BaseAdapter{

	private Context mContext;  
	private List<String> userwallpaperPaths;
	
	public UserImageAdapter(Context c,List<String> li){
		mContext=c;  
		userwallpaperPaths=li;
		System.gc();//优化内存溢出
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		return userwallpaperPaths.size();
	}

	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageview;  
		if(convertView==null){
			imageview=new ImageView(mContext);  
			imageview.setLayoutParams(new GridView.LayoutParams(85, 85));  
			imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);  
			imageview.setPadding(8,8,8,8);
		} else  
		{
			imageview=(ImageView) convertView;
			
		}
		if(userwallpaperPaths!=null){
			Bitmap bm=BitmapFactory.decodeFile(userwallpaperPaths.get(position).toString());
			if(bm!=null){
				Bitmap scaledBm=Bitmap.createScaledBitmap(bm, 85, 85,true);//将原图片缩略成固定大小，优化内存溢出
				bm.recycle();//及时清理处理过的Bitmap，优化内存溢出
		
				imageview.setImageBitmap(scaledBm);
			}
		}
		
		return imageview;
	}
	
	public List<String> getUserwallpaperPaths(){
		 return this.userwallpaperPaths;
	 }

}
